2000W / 48V LLC Battery-Charger  V1.00
xmc1_scu.c
Go to the documentation of this file.
1 
106 /*********************************************************************************************************************
107  * HEADER FILES
108  ********************************************************************************************************************/
109 #include <xmc_scu.h>
110 
111 #if UC_FAMILY == XMC1
112 
113 /*********************************************************************************************************************
114  * MACROS
115  ********************************************************************************************************************/
116 #define SCU_IRQ_NUM (3U)
117 #define SCU_GCU_PASSWD_PROT_ENABLE (195UL)
118 #define SCU_GCU_PASSWD_PROT_DISABLE (192UL)
121 #define XMC_SCU_CHECK_RTCCLKSRC(source) ( (source == XMC_SCU_CLOCK_RTCCLKSRC_DCO2) || \
122  (source == XMC_SCU_CLOCK_RTCCLKSRC_ERU_IOUT0) || \
123  (source == XMC_SCU_CLOCK_RTCCLKSRC_ACMP0_OUT) || \
124  (source == XMC_SCU_CLOCK_RTCCLKSRC_ACMP1_OUT) || \
125  (source == XMC_SCU_CLOCK_RTCCLKSRC_ACMP2_OUT) )
129 #define SCU_GENERAL_INTCR_INTSEL_Msk SCU_GENERAL_INTCR0_INTSEL0_Msk
131 #define SCU_GENERAL_INTCR_INTSEL_Size SCU_GENERAL_INTCR0_INTSEL1_Pos
134 #define ANA_TSE_T1 (0x10000F30U)
136 #define ANA_TSE_T2 (0x10000F31U)
138 #define DCO_ADJLO_T1 (0x10000F32U)
140 #define DCO_ADJLO_T2 (0x10000F33U)
143 #if UC_SERIES == XMC14
144 #define XMC_SCU_INTERRUPT_EVENT_MAX (64U)
145 #else
146 #define XMC_SCU_INTERRUPT_EVENT_MAX (32U)
147 #endif
148 
149 #if UC_SERIES == XMC14
150 #define DCO1_DIV2_FREQUENCY_KHZ_Q22_10 (48000U << 10)
152 #else
153 #define DCO1_DIV2_FREQUENCY_KHZ_Q24_8 (32000U << 8)
155 #endif
156 
157 #define ROM_BmiInstallationReq \
158  (*((uint32_t (**)(uint32_t requestedBmiValue))0x00000108U))
161 #define ROM_CalcTemperature \
162  (*((uint32_t (**)(void))0x0000010cU))
165 #define ROM_CalcTSEVAR \
166  (*((uint32_t (**)(uint32_t temperature))0x00000120U))
169 /*********************************************************************************************************************
170  * LOCAL DATA
171  ********************************************************************************************************************/
176 static XMC_SCU_INTERRUPT_EVENT_t event_masks[SCU_IRQ_NUM] =
177 {
178  (XMC_SCU_INTERRUPT_EVENT_FLASH_ERROR |
179  XMC_SCU_INTERRUPT_EVENT_FLASH_COMPLETED |
180  XMC_SCU_INTERRUPT_EVENT_PESRAM |
181  XMC_SCU_INTERRUPT_EVENT_PEUSIC0 |
182 #if defined(USIC1)
183  XMC_SCU_INTERRUPT_EVENT_PEUSIC1 |
184 #endif
185 #if defined(CAN)
186  XMC_SCU_INTERRUPT_EVENT_PEMCAN |
187 #endif
188 #if UC_SERIES == XMC14
189  XMC_SCU_INTERRUPT_EVENT_LOSS_EXT_CLOCK |
190 #endif
191  XMC_SCU_INTERRUPT_EVENT_LOCI),
193  (XMC_SCU_INTERRUPT_EVENT_STDBYCLKFAIL |
194 #if UC_SERIES == XMC14
195  XMC_SCU_INTERRUPT_EVENT_DCO1_OUT_SYNC |
196 #endif
197  XMC_SCU_INTERRUPT_EVENT_VDDPI |
198  XMC_SCU_INTERRUPT_EVENT_VDROP |
199  XMC_SCU_INTERRUPT_EVENT_VCLIP |
200  XMC_SCU_INTERRUPT_EVENT_TSE_DONE |
201  XMC_SCU_INTERRUPT_EVENT_TSE_HIGH |
202  XMC_SCU_INTERRUPT_EVENT_TSE_LOW |
203  XMC_SCU_INTERRUPT_EVENT_WDT_WARN |
204  XMC_SCU_INTERRUPT_EVENT_RTC_PERIODIC |
205  XMC_SCU_INTERRUPT_EVENT_RTC_ALARM |
206  XMC_SCU_INTERRUPT_EVENT_RTCCTR_UPDATED |
207  XMC_SCU_INTERRUPT_EVENT_RTCATIM0_UPDATED |
208  XMC_SCU_INTERRUPT_EVENT_RTCATIM1_UPDATED |
209  XMC_SCU_INTERRUPT_EVENT_RTCTIM0_UPDATED |
210  XMC_SCU_INTERRUPT_EVENT_RTCTIM1_UPDATED),
211 
212  (
213 #if UC_SERIES != XMC11
214  XMC_SCU_INTERRUPT_EVENT_ORC0 |
215  XMC_SCU_INTERRUPT_EVENT_ORC1 |
216  XMC_SCU_INTERRUPT_EVENT_ORC2 |
217  XMC_SCU_INTERRUPT_EVENT_ORC3 |
218  XMC_SCU_INTERRUPT_EVENT_ORC4 |
219  XMC_SCU_INTERRUPT_EVENT_ORC5 |
220  XMC_SCU_INTERRUPT_EVENT_ORC6 |
221  XMC_SCU_INTERRUPT_EVENT_ORC7 |
222 #endif
223 #if defined(COMPARATOR)
224  XMC_SCU_INTERRUPT_EVENT_ACMP0 |
225  XMC_SCU_INTERRUPT_EVENT_ACMP1 |
226  XMC_SCU_INTERRUPT_EVENT_ACMP2 |
227 #if UC_SERIES == XMC14
228  XMC_SCU_INTERRUPT_EVENT_ACMP3 |
229 #endif
230 #endif
231  0)
232 };
237 /*********************************************************************************************************************
238  * LOCAL ROUTINES
239  ********************************************************************************************************************/
240 /* Utility routine to perform frequency up scaling */
241 static void XMC_SCU_CLOCK_lFrequencyUpScaling(uint32_t curr_idiv, uint32_t idiv);
242 
243 /* Utility routine to perform frequency down scaling */
244 static void XMC_SCU_CLOCK_lFrequencyDownScaling(uint32_t curr_idiv, uint32_t idiv);
245 
246 /* Calculates the value which must be installed in ANATSEIx register to get indication in
247  SCU_INTERRUPT->SRRAW bit when the chip temperature is above/below some target/threshold. */
248 static uint32_t XMC_SCU_CalcTSEVAR(uint32_t temperature)
249 {
250  uint32_t limit;
251 
252  XMC_ASSERT("XMC_SCU_CalcTSEVAR: temperature out of range", (temperature >= 233U) && (temperature <= 388U));
253 
254  limit = ROM_CalcTSEVAR(temperature);
255  if (limit == 0U)
256  {
257  limit = ROM_CalcTSEVAR(temperature + 1U);
258  if (limit == 0U)
259  {
260  limit = ROM_CalcTSEVAR(temperature - 1U);
261  }
262  }
263 
264  return (limit);
265 }
266 
267 #if UC_SERIES == XMC14
268 /* This is a local function used to generate the delay until register get updated with new configured value. */
269 static void delay(uint32_t cycles)
270 {
271  while(cycles > 0U)
272  {
273  __NOP();
274  cycles--;
275  }
276 }
277 #endif
278 
279 /*********************************************************************************************************************
280  * API IMPLEMENTATION
281  ********************************************************************************************************************/
282  #ifdef XMC_ASSERT_ENABLE
283 /* API to verify SCU event weather it is valid event or not */
284 __STATIC_INLINE bool XMC_SCU_INTERRUPT_IsValidEvent(XMC_SCU_INTERRUPT_EVENT_t event)
285 {
286  return ((event == XMC_SCU_INTERRUPT_EVENT_WDT_WARN) ||
287  (event == XMC_SCU_INTERRUPT_EVENT_RTC_PERIODIC) ||
288  (event == XMC_SCU_INTERRUPT_EVENT_RTC_ALARM) ||
289  (event == XMC_SCU_INTERRUPT_EVENT_VDDPI) ||
290 #if defined(USIC1)
291  (event == XMC_SCU_INTERRUPT_EVENT_PEUSIC1) ||
292 #endif
293 #if defined(CAN)
294  (event == XMC_SCU_INTERRUPT_EVENT_PEMCAN) ||
295 #endif
296 #if UC_SERIES == XMC14
297  (event == XMC_SCU_INTERRUPT_EVENT_LOSS_EXT_CLOCK) ||
298  (event == XMC_SCU_INTERRUPT_EVENT_DCO1_OUT_SYNC) ||
299 #endif
300 #if defined(COMPARATOR)
301  (event == XMC_SCU_INTERRUPT_EVENT_ACMP0) ||
302  (event == XMC_SCU_INTERRUPT_EVENT_ACMP1) ||
303  (event == XMC_SCU_INTERRUPT_EVENT_ACMP2) ||
304 #if UC_SERIES == XMC14
305  (event == XMC_SCU_INTERRUPT_EVENT_ACMP3) ||
306 #endif
307 #endif
308  (event == XMC_SCU_INTERRUPT_EVENT_VDROP) ||
309 #if UC_SERIES != XMC11
310  (event == XMC_SCU_INTERRUPT_EVENT_ORC0) ||
311  (event == XMC_SCU_INTERRUPT_EVENT_ORC1) ||
312  (event == XMC_SCU_INTERRUPT_EVENT_ORC2) ||
313  (event == XMC_SCU_INTERRUPT_EVENT_ORC3) ||
314  (event == XMC_SCU_INTERRUPT_EVENT_ORC4) ||
315  (event == XMC_SCU_INTERRUPT_EVENT_ORC5) ||
316  (event == XMC_SCU_INTERRUPT_EVENT_ORC6) ||
317  (event == XMC_SCU_INTERRUPT_EVENT_ORC7) ||
318 #endif
319  (event == XMC_SCU_INTERRUPT_EVENT_LOCI) ||
320  (event == XMC_SCU_INTERRUPT_EVENT_PESRAM) ||
321  (event == XMC_SCU_INTERRUPT_EVENT_PEUSIC0) ||
322  (event == XMC_SCU_INTERRUPT_EVENT_FLASH_ERROR) ||
323  (event == XMC_SCU_INTERRUPT_EVENT_FLASH_COMPLETED) ||
324  (event == XMC_SCU_INTERRUPT_EVENT_VCLIP) ||
325  (event == XMC_SCU_INTERRUPT_EVENT_STDBYCLKFAIL) ||
326  (event == XMC_SCU_INTERRUPT_EVENT_RTCCTR_UPDATED) ||
327  (event == XMC_SCU_INTERRUPT_EVENT_RTCATIM0_UPDATED) ||
328  (event == XMC_SCU_INTERRUPT_EVENT_RTCATIM1_UPDATED) ||
329  (event == XMC_SCU_INTERRUPT_EVENT_RTCTIM0_UPDATED) ||
330  (event == XMC_SCU_INTERRUPT_EVENT_RTCTIM1_UPDATED) ||
331  (event == XMC_SCU_INTERRUPT_EVENT_TSE_DONE) ||
332  (event == XMC_SCU_INTERRUPT_EVENT_TSE_HIGH) ||
333  (event == XMC_SCU_INTERRUPT_EVENT_TSE_LOW));
334 }
335  #endif
336 
337 /* API to enable the SCU event */
338 void XMC_SCU_INTERRUPT_EnableEvent(const XMC_SCU_INTERRUPT_EVENT_t event)
339 {
340  SCU_INTERRUPT->SRMSK |= (uint32_t)event;
341 #if UC_SERIES == XMC14
342  SCU_INTERRUPT->SRMSK1 |= (uint32_t)(event >> 32U);
343 #endif
344 }
345 
346 /* API to disable the SCU event */
347 void XMC_SCU_INTERRUPT_DisableEvent(const XMC_SCU_INTERRUPT_EVENT_t event)
348 {
349  SCU_INTERRUPT->SRMSK &= ~(uint32_t)event;
350 #if UC_SERIES == XMC14
351  SCU_INTERRUPT->SRMSK1 &= (uint32_t)~(event >> 32U);
352 #endif
353 }
354 
355 /* API to trigger the SCU event */
356 void XMC_SCU_INTERRUPT_TriggerEvent(const XMC_SCU_INTERRUPT_EVENT_t event)
357 {
358  SCU_INTERRUPT->SRSET |= (uint32_t)event;
359 #if UC_SERIES == XMC14
360  SCU_INTERRUPT->SRSET1 |= (uint32_t)(event >> 32U);
361 #endif
362 }
363 
364 /* API to get the SCU event status */
365 XMC_SCU_INTERRUPT_EVENT_t XMC_SCU_INTERUPT_GetEventStatus(void)
366 {
367  XMC_SCU_INTERRUPT_EVENT_t tmp;
368 
369  tmp = SCU_INTERRUPT->SRRAW;
370 #if UC_SERIES == XMC14
371  tmp |= ((int64_t)SCU_INTERRUPT->SRRAW1 << 32U);
372 #endif
373  return (tmp);
374 }
375 
376 /* API to clear the SCU event status */
377 void XMC_SCU_INTERRUPT_ClearEventStatus(const XMC_SCU_INTERRUPT_EVENT_t event)
378 {
379  SCU_INTERRUPT->SRCLR |= (uint32_t)event;
380 #if UC_SERIES == XMC14
381  SCU_INTERRUPT->SRCLR1 |= (uint32_t)(event >> 32U);
382 #endif
383 }
384 
385 /* API to lock protected bitfields from being modified */
386 void XMC_SCU_LockProtectedBits(void)
387 {
389 }
390 
391 /* API to make protected bitfields available for modification */
393 {
395 
396  while(((SCU_GENERAL->PASSWD) & SCU_GENERAL_PASSWD_PROTS_Msk))
397  {
398  /* Loop until the lock is removed */
399  }
400 }
401 
402 /* API to initialize power supply monitoring unit */
403 void XMC_SCU_SupplyMonitorInit(const XMC_SCU_SUPPLYMONITOR_t *obj)
404 {
405  uint32_t anavdel;
406  uint32_t irqmask;
407 
408  anavdel = 0UL;
409 
410  anavdel |= (uint32_t)((obj-> ext_supply_threshold) << SCU_ANALOG_ANAVDEL_VDEL_SELECT_Pos);
411  anavdel |= (uint32_t)((obj->ext_supply_monitor_speed) << SCU_ANALOG_ANAVDEL_VDEL_TIM_ADJ_Pos);
413  if(true == (obj->enable_at_init))
414  {
415  anavdel |= (uint32_t)SCU_ANALOG_ANAVDEL_VDEL_EN_Msk;
416  }
417  SCU_ANALOG->ANAVDEL = (uint16_t) anavdel;
418 
419  irqmask = 0UL;
420 
421  if(true == (obj->enable_prewarning_int))
422  {
423  irqmask |= (uint32_t)SCU_INTERRUPT_SRMSK_VDDPI_Msk;
424  }
425 
426  if(true == (obj->enable_vdrop_int))
427  {
428  irqmask |= (uint32_t)SCU_INTERRUPT_SRMSK_VDROPI_Msk;
429  }
430 
431  if(true == (obj->enable_vclip_int))
432  {
433  irqmask |= (uint32_t)SCU_INTERRUPT_SRMSK_VCLIPI_Msk;
434  }
435  SCU_INTERRUPT->SRMSK |= (uint32_t)irqmask;
436 }
437 
438 /* API to program lower temperature limit */
440 {
442 
443  limit = XMC_SCU_CalcTSEVAR(limit);
444 
445  if (limit != 0)
446  {
447  SCU_ANALOG->ANATSEIL = (uint16_t)limit;
448  }
449  else
450  {
451  status = XMC_SCU_STATUS_ERROR;
452  }
453 
454  return (status);
455 }
456 
457 /* API to program higher temperature limit */
459 {
461 
462  limit = XMC_SCU_CalcTSEVAR(limit);
463 
464  if (limit != 0)
465  {
466  SCU_ANALOG->ANATSEIH = (uint16_t)limit;
467  }
468  else
469  {
470  status = XMC_SCU_STATUS_ERROR;
471  }
472 
473  return (status);
474 }
475 
476 /* API to program temperature limits as raw digital values into temperature sensor */
477 void XMC_SCU_SetRawTempLimits(const uint32_t lower_temp, const uint32_t upper_temp)
478 {
479  SCU_ANALOG->ANATSEIH = (uint16_t)(upper_temp & SCU_ANALOG_ANATSEIH_TSE_IH_Msk);
480  SCU_ANALOG->ANATSEIL = (uint16_t)(lower_temp & SCU_ANALOG_ANATSEIL_TSE_IL_Msk);
481 }
482 
483 /* API to start temperature measurement */
485 {
486  SCU_ANALOG->ANATSECTRL |= (uint16_t)SCU_ANALOG_ANATSECTRL_TSE_EN_Msk;
487 }
488 
489 /* API to stop temperature measurement */
491 {
492  SCU_ANALOG->ANATSECTRL &= (uint16_t)~SCU_ANALOG_ANATSECTRL_TSE_EN_Msk;
493 }
494 
495 /* API to check if the temperature has gone past the ceiling */
496 bool XMC_SCU_HighTemperature(void)
497 {
499 }
500 
501 /* API to check if the temperature is lower than normal */
502 bool XMC_SCU_LowTemperature(void)
503 {
505 }
506 
507 /* API to retrieve the device temperature */
508 uint32_t XMC_SCU_GetTemperature(void)
509 {
510  uint32_t temperature;
511 
512  temperature = (uint32_t)(SCU_ANALOG->ANATSEMON);
513 
514  return (temperature);
515 }
517 /* Calculates the die temperature value using ROM function */
518 uint32_t XMC_SCU_CalcTemperature(void)
519 {
520  return (ROM_CalcTemperature());
521 }
523 
524 /* API which initializes the clock tree ofthe device */
525 void XMC_SCU_CLOCK_Init(const XMC_SCU_CLOCK_CONFIG_t *const config)
526 {
527  /* Remove protection */
529 
530 #if (UC_SERIES == XMC14)
531  /* OSCHP source selection - OSC mode */
532 
533  if (config->oschp_mode != XMC_SCU_CLOCK_OSCHP_MODE_DISABLED)
534  {
535  if (OSCHP_GetFrequency() > 20000000U)
536  {
537  SCU_ANALOG->ANAOSCHPCTRL |= (uint16_t)SCU_ANALOG_ANAOSCHPCTRL_HYSCTRL_Msk;
538  }
539 
540  SCU_ANALOG->ANAOSCHPCTRL = (uint16_t)(SCU_ANALOG->ANAOSCHPCTRL & ~(SCU_ANALOG_ANAOSCHPCTRL_SHBY_Msk | SCU_ANALOG_ANAOSCHPCTRL_MODE_Msk)) |
541  config->oschp_mode;
542 
543  do
544  {
545  /* Restart OSC_HP oscillator watchdog */
547 
548  /* Enable OSC_HP oscillator watchdog*/
551 
552  /* Wait a few DCO2 cycles for the update of the clock detection result */
553  delay(2500);
554 
555  /* check clock is ok */
556  }
558  }
559  else /* (config->oschp_mode == XMC_SCU_CLOCK_OSCHP_MODE_DISABLED) */
560  {
562  }
563 
564  SCU_ANALOG->ANAOSCLPCTRL = (uint16_t)config->osclp_mode;
565 
566  SCU_CLK->CLKCR1 = (SCU_CLK->CLKCR1 & ~SCU_CLK_CLKCR1_DCLKSEL_Msk) |
567  config->dclk_src;
568 
569 #endif
570  /* Update PCLK selection mux. */
571  SCU_CLK->CLKCR = (SCU_CLK->CLKCR & (uint32_t)~(SCU_CLK_CLKCR_PCLKSEL_Msk | SCU_CLK_CLKCR_RTCCLKSEL_Msk)) |
572  config->rtc_src |
573  config->pclk_src;
574 
575  /* Close the lock opened above. */
577 
578  /* Update the dividers now */
579  XMC_SCU_CLOCK_ScaleMCLKFrequency(config->idiv, config->fdiv);
580 
581 }
582 
583 /* API which selects one of the available parent clock nodes for a given child clock node */
584 void XMC_SCU_CLOCK_SetRtcClockSource(const XMC_SCU_CLOCK_RTCCLKSRC_t source)
585 {
586  XMC_ASSERT("XMC_SCU_CLOCK_SetRtcSourceClock:Wrong Parent Clock", XMC_SCU_CHECK_RTCCLKSRC(source));
587 
589 
590  SCU_CLK->CLKCR = (SCU_CLK->CLKCR & (uint32_t)~SCU_CLK_CLKCR_RTCCLKSEL_Msk) |
591  source;
592 
594 }
595 
596 /* API to program the divider placed between fperiph and its parent */
597 void XMC_SCU_CLOCK_SetFastPeripheralClockSource(const XMC_SCU_CLOCK_PCLKSRC_t source)
598 {
600 
601  SCU_CLK->CLKCR = (SCU_CLK->CLKCR & (uint32_t)~SCU_CLK_CLKCR_PCLKSEL_Msk) |
602  source;
603 
605 }
606 
607 /* API which gates a clock node at its source */
608 void XMC_SCU_CLOCK_GatePeripheralClock(const XMC_SCU_PERIPHERAL_CLOCK_t peripheral)
609 {
611  SCU_CLK->CGATSET0 |= (uint32_t)peripheral;
613 }
614 
615 /* API which ungates a clock note at its source */
616 void XMC_SCU_CLOCK_UngatePeripheralClock(const XMC_SCU_PERIPHERAL_CLOCK_t peripheral)
617 {
619  SCU_CLK->CGATCLR0 |= (uint32_t)peripheral;
620  while ((SCU_CLK->CLKCR) & SCU_CLK_CLKCR_VDDC2LOW_Msk)
621  {
622  /* Wait voltage suply stabilization */
623  }
625 }
626 
627 /* Checks the status of peripheral clock gating */
628 bool XMC_SCU_CLOCK_IsPeripheralClockGated(const XMC_SCU_PERIPHERAL_CLOCK_t peripheral)
629 {
630  return (bool)((SCU_CLK->CGATSTAT0 & peripheral) != 0);
631 }
632 
633 /* This API configures main clock (MCLK) frequency to requested frequency value. */
634 void XMC_SCU_CLOCK_SetMCLKFrequency(uint32_t freq_khz)
635 {
636  uint32_t ratio;
637  uint32_t ratio_int;
638  uint32_t ratio_frac;
639 
640 #if UC_SERIES == XMC14
641  if (((SCU_CLK->CLKCR1) & SCU_CLK_CLKCR1_DCLKSEL_Msk) == 0U)
642  {
643  ratio = DCO1_DIV2_FREQUENCY_KHZ_Q22_10 / freq_khz;
644  }
645  else
646  {
647  ratio = ((OSCHP_GetFrequency() / 1000U) << 10U) / freq_khz;
648  }
649 
650  /* Manage overflow */
651  if (ratio > 0xffffffU)
652  {
653  ratio = 0xffffffU;
654  }
655 
656  ratio_int = ratio >> 10U;
657  ratio_frac = ratio & 0x3ffU;
658 #else
659  ratio = DCO1_DIV2_FREQUENCY_KHZ_Q24_8 / freq_khz;
660 
661  /* Manage overflow */
662  if (ratio > 0xffffU)
663  {
664  ratio = 0xffffU;
665  }
666 
667  ratio_int = ratio >> 8U;
668  ratio_frac = ratio & 0xffU;
669 #endif
670 
671  XMC_SCU_CLOCK_ScaleMCLKFrequency(ratio_int, ratio_frac);
672 }
673 
674 
675 /* A utility routine which updates the fractional dividers in steps */
676 void XMC_SCU_CLOCK_ScaleMCLKFrequency(uint32_t idiv, uint32_t fdiv)
677 {
678  /* Find out current and target value of idiv */
679  uint32_t curr_idiv;
680 
682 
683  /* Take a snapshot of value already programmed into IDIV */
684  curr_idiv = (SCU_CLK->CLKCR & SCU_CLK_CLKCR_IDIV_Msk) >> SCU_CLK_CLKCR_IDIV_Pos;
685 
686 #if (UC_SERIES == XMC14)
687  SCU_CLK->CLKCR1 = (SCU_CLK->CLKCR1 & (uint32_t)~(SCU_CLK_CLKCR1_FDIV_Msk)) |
688  (uint32_t)((fdiv >> 8U) << SCU_CLK_CLKCR1_FDIV_Pos);
689 
690  SCU_CLK->CLKCR = (SCU_CLK->CLKCR & (uint32_t)~(SCU_CLK_CLKCR_FDIV_Msk | SCU_CLK_CLKCR_CNTADJ_Msk)) |
691  (uint32_t)((fdiv & 0xffU) << SCU_CLK_CLKCR_FDIV_Pos) |
692  (uint32_t)(1023UL << SCU_CLK_CLKCR_CNTADJ_Pos);
693 #else
694  SCU_CLK->CLKCR = (SCU_CLK->CLKCR & (uint32_t)~(SCU_CLK_CLKCR_FDIV_Msk | SCU_CLK_CLKCR_CNTADJ_Msk)) |
695  (uint32_t)(fdiv << SCU_CLK_CLKCR_FDIV_Pos) |
696  (uint32_t)(1023UL << SCU_CLK_CLKCR_CNTADJ_Pos);
697 #endif
698 
699  while ((SCU_CLK->CLKCR)& SCU_CLK_CLKCR_VDDC2LOW_Msk)
700  {
701  /* Spin until the core supply stabilizes */
702  }
703 
704  if(curr_idiv <= idiv)
705  {
706  /* Requested IDIV is greater than currently programmed IDIV. So downscale the frequency */
707  XMC_SCU_CLOCK_lFrequencyDownScaling(curr_idiv, idiv);
708  }
709  else
710  {
711  /* Requested IDIV is lower than currently programmed IDIV. So upscale the frequency */
712  XMC_SCU_CLOCK_lFrequencyUpScaling(curr_idiv, idiv);
713  }
714 
715  SCU_CLK->CLKCR = (SCU_CLK->CLKCR & (uint32_t)~(SCU_CLK_CLKCR_IDIV_Msk | SCU_CLK_CLKCR_CNTADJ_Msk)) |
716  (uint32_t)(idiv << SCU_CLK_CLKCR_IDIV_Pos) | (uint32_t)(1023UL << SCU_CLK_CLKCR_CNTADJ_Pos);
717 
718  while ((SCU_CLK->CLKCR) & SCU_CLK_CLKCR_VDDC2LOW_Msk)
719  {
720  /* Wait voltage suply stabilization */
721  }
722 
724 
726 
727 }
728 
729 /* Utility routine to perform frequency up scaling */
730 static void XMC_SCU_CLOCK_lFrequencyUpScaling(uint32_t curr_idiv, uint32_t target_idiv)
731 {
732  while (curr_idiv > (target_idiv * 4UL))
733  {
734  curr_idiv = (uint32_t)(curr_idiv >> 2UL); /* Divide by 4. */
735 
736  SCU_CLK->CLKCR = (SCU_CLK->CLKCR & (uint32_t)~(SCU_CLK_CLKCR_IDIV_Msk | SCU_CLK_CLKCR_CNTADJ_Msk)) |
737  (uint32_t)(curr_idiv << SCU_CLK_CLKCR_IDIV_Pos) |
738  (uint32_t)(1023UL << SCU_CLK_CLKCR_CNTADJ_Pos);
739 
740  while (SCU_CLK->CLKCR & SCU_CLK_CLKCR_VDDC2LOW_Msk)
741  {
742  /* Wait voltage suply stabilization */
743  }
744  }
745 }
746 
747 /* Utility routine to perform frequency down scaling */
748 static void XMC_SCU_CLOCK_lFrequencyDownScaling(uint32_t curr_idiv, uint32_t target_idiv)
749 {
751  while ((curr_idiv * 4UL) < target_idiv)
752  {
753  if(0U == curr_idiv)
754  {
755  curr_idiv = 1U;
756  }
757  curr_idiv = (uint32_t)(curr_idiv << 2UL); /* Multiply by 4. */
758  SCU_CLK->CLKCR = (SCU_CLK->CLKCR & (uint32_t)~(SCU_CLK_CLKCR_IDIV_Msk | SCU_CLK_CLKCR_CNTADJ_Msk)) |
759  (uint32_t)(curr_idiv << SCU_CLK_CLKCR_IDIV_Pos) |
760  (uint32_t)(1023UL << SCU_CLK_CLKCR_CNTADJ_Pos);
761 
762  while (SCU_CLK->CLKCR & SCU_CLK_CLKCR_VDDC2LOW_Msk)
763  {
764  /* Wait voltage suply stabilization */
765  }
766  }
767 }
769 /*
770  * API to retrieve clock frequency of peripherals on the peripheral bus using a shared functional clock
771  */
773 {
774  return (SystemCoreClock);
775 }
776 
777 /* Provides the clock frequency of peripherals on the peripheral bus that are using a shared functional clock */
779 {
781 }
782 
783 /* DCO1 clock frequency can be calibrated during runtime to achieve a better accuracy */
784 void XMC_SCU_CLOCK_CalibrateOscillatorOnTemperature(int32_t temperature)
785 {
786  int32_t a;
787  int32_t b;
788  int32_t d;
789  int32_t e;
790  int32_t offset;
791 
792  a = *((uint8_t*)DCO_ADJLO_T2);
793  b = *((uint8_t*)DCO_ADJLO_T1);
794  d = *((uint8_t*)ANA_TSE_T1);
795  e = *((uint8_t*)ANA_TSE_T2);
796 
797  offset = b + (((a - b) * (temperature - d)) / (e - d));
799 
801  SCU_ANALOG->ANAOFFSET = (uint16_t)offset;
803 }
805 /*
806  * API to assign the event handler function to be executed on occurrence of the selected event
807  */
809 {
810  uint32_t index;
811  XMC_SCU_STATUS_t status;
812 
813  XMC_ASSERT("XMC_SCU_INTERRUPT_SetEventHandler: Invalid event", XMC_SCU_INTERRUPT_IsValidEvent(event));
814  XMC_ASSERT("XMC_SCU_INTERRUPT_SetEventHandler: Invalid handler", handler != NULL);
815 
816  index = 0U;
817 
818  while (((event & ((XMC_SCU_INTERRUPT_EVENT_t)1 << index)) == 0U) && (index < XMC_SCU_INTERRUPT_EVENT_MAX))
819  {
820  index++;
821  }
822 
823  if (index == XMC_SCU_INTERRUPT_EVENT_MAX)
824  {
825  status = XMC_SCU_STATUS_ERROR;
826  }
827  else
828  {
829  event_handler_list[index] = handler;
830  status = XMC_SCU_STATUS_OK;
831  }
832 
833  return (status);
834 }
835 
836 /*
837  * A common function to execute callback functions for multiple events
838  */
839 void XMC_SCU_IRQHandler(uint32_t sr_num)
840 {
841  XMC_ASSERT("XMC_SCU_IRQHandler: Invalid sr_num", sr_num < SCU_IRQ_NUM);
842 
843  uint32_t index;
844  XMC_SCU_INTERRUPT_EVENT_t event;
845  XMC_SCU_INTERRUPT_EVENT_HANDLER_t event_handler;
846 
847  index = 0U;
848  event = XMC_SCU_INTERUPT_GetEventStatus() & event_masks[sr_num];
850 
851  while ((event != 0) && (index < XMC_SCU_INTERRUPT_EVENT_MAX))
852  {
853  if ((event & ((XMC_SCU_INTERRUPT_EVENT_t)1 << index)) != 0U)
854  {
855  event &= ~((XMC_SCU_INTERRUPT_EVENT_t)1 << index);
856  event_handler = event_handler_list[index];
857  if (event_handler != NULL)
858  {
859  event_handler();
860  }
861 
862  /* break; XMC1: Only PULSE interrupts */
863  }
864  index++;
865  }
866 }
867 
868 /* Install BMI */
869 uint32_t XMC_SCU_SetBMI(uint32_t flags, uint8_t timeout)
870 {
871 #if (UC_SERIES == XMC14)
872  return ROM_BmiInstallationReq((flags & 0x0fffU) | ((timeout << 12) & 0xf000U));
873 #else
874  return ROM_BmiInstallationReq((flags & 0x07ffU) | ((timeout << 12) & 0xf000U) | 0x08c0U);
875 #endif
876 }
877 
878 #if (UC_SERIES == XMC14)
879 /* DCO1 clock frequency can be calibrated during runtime to achieve a better accuracy */
880 void XMC_SCU_CLOCK_EnableDCO1ExtRefCalibration(XMC_SCU_CLOCK_SYNC_CLKSRC_t sync_clk, uint32_t prescaler, uint32_t syn_preload)
881 {
882 
884  SCU_ANALOG->ANASYNC2 = (uint16_t)(prescaler << SCU_ANALOG_ANASYNC2_PRESCALER_Pos);
885  SCU_ANALOG->ANASYNC1 = (uint16_t)(syn_preload |
886  sync_clk |
888 
890 
891 }
892 
893 /* This function stops the automatic DCO1 calibration based on the selected clock source */
895 {
897  SCU_ANALOG->ANASYNC2 = 0U;
898  SCU_ANALOG->ANASYNC1 = 0U;
900 }
901 
902 /* This functions checks the status of the synchronisation */
904 {
905  return (bool)((SCU_ANALOG->ANASYNC2 & SCU_ANALOG_ANASYNC2_SYNC_READY_Msk) != 0U);
906 }
907 
908 /* This function selects service request source for a NVIC interrupt node */
909 void XMC_SCU_SetInterruptControl(uint8_t irq_number, XMC_SCU_IRQCTRL_t source)
910 {
911  XMC_ASSERT("XMC_SCU_SetInterruptControl: Invalid irq_number", irq_number < 32);
912  XMC_ASSERT("XMC_SCU_SetInterruptControl: Invalid source", (source >> 8) == irq_number);
913 
914  source &= 0x3U;
915  if (irq_number < 16U)
916  {
917  SCU_GENERAL->INTCR0 = (SCU_GENERAL->INTCR0 & ~(SCU_GENERAL_INTCR_INTSEL_Msk << (irq_number * SCU_GENERAL_INTCR_INTSEL_Size))) |
918  (source << (irq_number * SCU_GENERAL_INTCR_INTSEL_Size));
919  }
920  else
921  {
922  irq_number &= 0x0fU;
924  (source << (irq_number * SCU_GENERAL_INTCR_INTSEL_Size));
925  }
926 }
927 
928 #endif
929 #endif /* UC_FAMILY == XMC1 */
void(* XMC_SCU_INTERRUPT_EVENT_HANDLER_t)(void)
Definition: xmc_scu.h:187
uint32_t XMC_SCU_CLOCK_GetFastPeripheralClockFrequency(void)
Definition: xmc1_scu.c:798
#define CAN
Definition: XMC1400.h:8832
#define SCU_CLK_CLKCR_PCLKSEL_Msk
Definition: XMC1400.h:4448
void XMC_SCU_INTERRUPT_DisableEvent(const XMC_SCU_INTERRUPT_EVENT_t event)
Definition: xmc1_scu.c:367
uint32_t XMC_SCU_CLOCK_GetPeripheralClockFrequency(void)
Definition: xmc1_scu.c:792
enum XMC_SCU_STATUS XMC_SCU_STATUS_t
void XMC_SCU_LockProtectedBits(void)
Definition: xmc1_scu.c:406
void XMC_SCU_SetInterruptControl(uint8_t irq_number, XMC_SCU_IRQCTRL_t source)
Definition: xmc1_scu.c:929
static uint32_t XMC_SCU_CalcTSEVAR(uint32_t temperature)
Definition: xmc1_scu.c:268
#define __NOP()
No Operation.
Definition: cmsis_gcc.h:777
#define SCU_ANALOG_ANATSECTRL_TSE_EN_Msk
Definition: XMC1400.h:4740
void XMC_SCU_UnlockProtectedBits(void)
Definition: xmc1_scu.c:412
#define SCU_INTERRUPT_SRMSK_VDROPI_Msk
Definition: XMC1400.h:4202
void XMC_SCU_StopTempMeasurement(void)
Definition: xmc1_scu.c:510
void XMC_SCU_CLOCK_UngatePeripheralClock(const XMC_SCU_PERIPHERAL_CLOCK_t peripheral)
Definition: xmc1_scu.c:636
#define SCU_CLK_CLKCR_IDIV_Pos
Definition: XMC1400.h:4445
#define DCO1_DIV2_FREQUENCY_KHZ_Q22_10
Definition: xmc1_scu.c:166
#define SCU_ANALOG_ANASYNC2_PRESCALER_Pos
Definition: XMC1400.h:4775
#define SCU_ANALOG_ANATSEIH_TSE_IH_Msk
Definition: XMC1400.h:4744
#define XMC14
Definition: xmc_device.h:75
#define SCU_ANALOG_ANAVDEL_VDEL_EN_Msk
Definition: XMC1400.h:4760
bool XMC_SCU_CLOCK_IsPeripheralClockGated(const XMC_SCU_PERIPHERAL_CLOCK_t peripheral)
Definition: xmc1_scu.c:648
void XMC_SCU_INTERRUPT_TriggerEvent(const XMC_SCU_INTERRUPT_EVENT_t event)
Definition: xmc1_scu.c:376
#define SCU_GENERAL_INTCR_INTSEL_Msk
Definition: xmc1_scu.c:139
#define SCU_ANALOG_ANAVDEL_VDEL_TIM_ADJ_Pos
Definition: XMC1400.h:4757
#define SCU_IRQ_NUM
SCU low level driver API prototype definition for XMC1 family of microcontrollers ...
Definition: xmc1_scu.c:116
void XMC_SCU_SetRawTempLimits(const uint32_t lower_temp, const uint32_t upper_temp)
Definition: xmc1_scu.c:497
void XMC_SCU_INTERRUPT_ClearEventStatus(const XMC_SCU_INTERRUPT_EVENT_t event)
Definition: xmc1_scu.c:397
#define SCU_GCU_PASSWD_PROT_ENABLE
Definition: xmc1_scu.c:117
#define SCU_GCU_PASSWD_PROT_DISABLE
Definition: xmc1_scu.c:118
#define SCU_ANALOG
Definition: XMC1400.h:8845
uint32_t XMC_SCU_CalcTemperature(void)
Definition: xmc1_scu.c:538
#define SCU_CLK_CLKCR1_FDIV_Msk
Definition: XMC1400.h:4588
#define SCU_GENERAL_PASSWD_PROTS_Msk
Definition: XMC1400.h:4014
XMC_SCU_STATUS_t XMC_SCU_INTERRUPT_SetEventHandler(XMC_SCU_INTERRUPT_EVENT_t event, XMC_SCU_INTERRUPT_EVENT_HANDLER_t handler)
Definition: xmc1_scu.c:828
#define ANA_TSE_T1
Definition: xmc1_scu.c:146
#define XMC_ASSERT(msg, exp)
Definition: xmc_common.h:132
void XMC_SCU_CLOCK_ScaleMCLKFrequency(uint32_t idiv, uint32_t fdiv)
Definition: xmc1_scu.c:696
XMC_SCU_STATUS_t XMC_SCU_SetTempLowLimit(uint32_t limit)
Definition: xmc1_scu.c:459
#define SCU_CLK_CLKCR_FDIV_Pos
Definition: XMC1400.h:4443
#define SCU_CLK_CLKCR_PCLKSEL_Pos
Definition: XMC1400.h:4447
#define SCU_CLK_CLKCR_RTCCLKSEL_Msk
Definition: XMC1400.h:4450
#define SCU_ANALOG_ANAOSCHPCTRL_SHBY_Msk
Definition: XMC1400.h:4786
#define XMC11
Definition: xmc_device.h:78
void XMC_SCU_CLOCK_Init(const XMC_SCU_CLOCK_CONFIG_t *const config)
Definition: xmc1_scu.c:545
#define SCU_INTERRUPT
Definition: XMC1400.h:8838
void XMC_SCU_CLOCK_GatePeripheralClock(const XMC_SCU_PERIPHERAL_CLOCK_t peripheral)
Definition: xmc1_scu.c:628
void XMC_SCU_CLOCK_CalibrateOscillatorOnTemperature(int32_t temperature)
Definition: xmc1_scu.c:804
bool XMC_SCU_CLOCK_IsDCO1ExtRefCalibrationReady(void)
Definition: xmc1_scu.c:923
static XMC_SCU_INTERRUPT_EVENT_t event_masks[SCU_IRQ_NUM]
Definition: xmc1_scu.c:196
void XMC_SCU_SupplyMonitorInit(const XMC_SCU_SUPPLYMONITOR_t *obj)
Definition: xmc1_scu.c:423
#define SCU_INTERRUPT_SRRAW_TSE_LOW_Msk
Definition: XMC1400.h:4188
uint32_t XMC_SCU_SetBMI(uint32_t flags, uint8_t timeout)
Definition: xmc1_scu.c:889
bool XMC_SCU_LowTemperature(void)
Definition: xmc1_scu.c:522
static XMC_SCU_INTERRUPT_EVENT_HANDLER_t event_handler_list[XMC_SCU_INTERRUPT_EVENT_MAX]
Definition: xmc1_scu.c:192
void XMC_SCU_IRQHandler(uint32_t sr_num)
Definition: xmc1_scu.c:859
#define SCU_ANALOG_ANASYNC2_SYNC_READY_Msk
Definition: XMC1400.h:4778
#define SCU_CLK_CLKCR1_FDIV_Pos
Definition: XMC1400.h:4587
#define ROM_CalcTSEVAR
Definition: xmc1_scu.c:184
#define SCU_INTERRUPT_SRRAW1_LOECI_Msk
Definition: XMC1400.h:4380
static void XMC_SCU_CLOCK_lFrequencyDownScaling(uint32_t curr_idiv, uint32_t idiv)
Definition: xmc1_scu.c:768
#define DCO_ADJLO_T2
Definition: xmc1_scu.c:155
#define SCU_CLK
Definition: XMC1400.h:8840
#define SCU_CLK_CLKCR_CNTADJ_Msk
Definition: XMC1400.h:4452
#define SCU_ANALOG_ANAOSCHPCTRL_HYSCTRL_Msk
Definition: XMC1400.h:4792
#define SCU_GENERAL
Definition: XMC1400.h:8837
#define SCU_CLK_OSCCSR_XOWDEN_Msk
Definition: XMC1400.h:4584
#define XMC_SCU_CHECK_RTCCLKSRC(source)
Definition: xmc1_scu.c:121
void XMC_SCU_CLOCK_SetMCLKFrequency(uint32_t freq_khz)
Definition: xmc1_scu.c:654
#define SCU_ANALOG_ANATSEIL_TSE_IL_Msk
Definition: XMC1400.h:4748
#define ANA_TSE_T2
Definition: xmc1_scu.c:149
bool XMC_SCU_HighTemperature(void)
Definition: xmc1_scu.c:516
#define DCO_ADJLO_T1
Definition: xmc1_scu.c:152
#define SCU_GENERAL_INTCR_INTSEL_Size
Definition: xmc1_scu.c:142
void XMC_SCU_StartTempMeasurement(void)
Definition: xmc1_scu.c:504
#define SCU_CLK_CLKCR_IDIV_Msk
Definition: XMC1400.h:4446
#define ROM_BmiInstallationReq
Definition: xmc1_scu.c:174
#define SCU_INTERRUPT_SRMSK_VCLIPI_Msk
Definition: XMC1400.h:4228
uint32_t XMC_SCU_GetTemperature(void)
Definition: xmc1_scu.c:528
#define SCU_ANALOG_ANAOFFSET_ADJL_OFFSET_Msk
Definition: XMC1400.h:4764
uint32_t OSCHP_GetFrequency(void)
Returns frequency of the high performace oscillator User needs to overload this function to return th...
#define SCU_ANALOG_ANASYNC1_SYNC_DCO_EN_Msk
Definition: XMC1400.h:4770
#define SCU_CLK_CLKCR1_DCLKSEL_Msk
Definition: XMC1400.h:4592
#define SCU_INTERRUPT_SRMSK_VDDPI_Msk
Definition: XMC1400.h:4194
#define USIC1
Definition: XMC1400.h:8826
#define SCU_INTERRUPT_SRCLR1_LOECI_Msk
Definition: XMC1400.h:4404
#define SCU_CLK_CLKCR_CNTADJ_Pos
Definition: XMC1400.h:4451
#define ROM_CalcTemperature
Definition: xmc1_scu.c:179
#define SCU_ANALOG_ANAOSCHPCTRL_MODE_Msk
Definition: XMC1400.h:4790
XMC_SCU_STATUS_t XMC_SCU_SetTempHighLimit(uint32_t limit)
Definition: xmc1_scu.c:478
XMC_SCU_INTERRUPT_EVENT_t XMC_SCU_INTERUPT_GetEventStatus(void)
Definition: xmc1_scu.c:385
void XMC_SCU_CLOCK_SetRtcClockSource(const XMC_SCU_CLOCK_RTCCLKSRC_t source)
Definition: xmc1_scu.c:604
void XMC_SCU_CLOCK_SetFastPeripheralClockSource(const XMC_SCU_CLOCK_PCLKSRC_t source)
Definition: xmc1_scu.c:617
#define __STATIC_INLINE
Definition: cmsis_gcc.h:47
#define SCU_ANALOG_ANAVDEL_VDEL_SELECT_Pos
Definition: XMC1400.h:4755
uint32_t SystemCoreClock
#define SCU_CLK_CLKCR_VDDC2LOW_Msk
Definition: XMC1400.h:4454
void XMC_SCU_CLOCK_EnableDCO1ExtRefCalibration(XMC_SCU_CLOCK_SYNC_CLKSRC_t sync_clk, uint32_t prescaler, uint32_t syn_preload)
Definition: xmc1_scu.c:900
void SystemCoreClockUpdate(void)
Update SystemCoreClock variable.
static void XMC_SCU_CLOCK_lFrequencyUpScaling(uint32_t curr_idiv, uint32_t idiv)
Definition: xmc1_scu.c:750
void XMC_SCU_INTERRUPT_EnableEvent(const XMC_SCU_INTERRUPT_EVENT_t event)
Definition: xmc1_scu.c:358
#define SCU_CLK_CLKCR_FDIV_Msk
Definition: XMC1400.h:4444
void XMC_SCU_CLOCK_DisableDCO1ExtRefCalibration(void)
Definition: xmc1_scu.c:914
#define COMPARATOR
Definition: XMC1400.h:8843
#define XMC_SCU_INTERRUPT_EVENT_MAX
Definition: xmc1_scu.c:160
#define SCU_INTERRUPT_SRRAW_TSE_HIGH_Msk
Definition: XMC1400.h:4186
static void delay(uint32_t cycles)
Definition: xmc1_scu.c:289